package com.yyds.yygh.hospclient.receiver;

import com.rabbitmq.client.Channel;
import com.yyds.yygh.hospclient.service.ScheduleService;
import com.yyds.yygh.model.hosp.Schedule;
import com.yyds.yygh.rabbit.MqConst;
import com.yyds.yygh.rabbit.RabbitService;
import com.yyds.yygh.vo.msm.MsmVo;
import com.yyds.yygh.vo.order.OrderMqVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HospitalReceiver {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private RabbitService rabbitService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER , durable = "true"),
            exchange =  @Exchange(value = MqConst.EXCHANGE_DIRECT_ORDER),
            key = {MqConst.ROUTING_ORDER}))
    public void receiver(OrderMqVo orderMqVo , Message message , Channel channel){
        if (orderMqVo.getAvailableNumber()!=null){
            //预约成功
            Schedule schedule = scheduleService.getById(orderMqVo.getScheduleId());
            schedule.setReservedNumber(orderMqVo.getReservedNumber());
            schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
            scheduleService.update(schedule);
        }else {
            //取消下单
            Schedule schedule = scheduleService.getById(orderMqVo.getScheduleId());
            int AvailableNumber = schedule.getAvailableNumber().intValue() + 1;
            schedule.setAvailableNumber(AvailableNumber);
            scheduleService.update(schedule);
        }
        //发送短信
        MsmVo msmVo = orderMqVo.getMsmVo();
        if (null != msmVo){
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM,MqConst.ROUTING_MSM_ITEM,msmVo);
        }
    }
}
